﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkflowFramework
{
    public class BranchFlowStep : FlowStepBase
    {
        public IWorkFlowExecutor TrueBranch
        {
            get
            {
                return _trueBranch;
            }
        }
        public IWorkFlowExecutor FalseBranch
        {
            get
            {
                return _falseBranch;
            }
        }
        private readonly Func<bool> _condition;
        private readonly IWorkFlowExecutor _trueBranch;
        private readonly IWorkFlowExecutor _falseBranch;

        public BranchFlowStep(Func<bool> condition, IWorkFlowExecutor trueBranch, IWorkFlowExecutor falseBranch)
        {
            _condition = condition;
            _trueBranch = trueBranch;
            _falseBranch = falseBranch;
            StepName = $"Branch[{condition.Method.Name}]";
        }

        public override string StepName { get; }

        public override async Task<FlowStepResult> ExecuteAsync()
        {
            var executor = _condition() ? _trueBranch : _falseBranch;
            if (executor != null)
            {
                await executor.ExecuteFlowAsync();
            }
            return FlowStepResult.Success();
        }
    }
}
